﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Membership
{
    public static class CreateMember
    {
        public static Adams.Admin.DataTransfer.Membership.CreateMember.PageLoad PageLoad(int id)
        {
            var model = DataAccess.Membership.CreateMember.PageLoad(id);
            if(model.Member.ID == 0)
            {
                model.Member.IsActive = true;
            }
            return model;
        }

        public static DataTransfer.FormOutput SaveMember(DataTransfer.Membership.CreateMember.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var Memberinfo = DataAccess.Membership.CreateMember.GetMemberInfo(input.ID.ToInt(), input.Email);
                if (input.ID.ToInt() > 0)
                {
                    if (!Shared.Str.IsEqual(input.Email, Memberinfo.Email))
                    {
                        if (Memberinfo.Exists)
                        {
                            output.InvalidFields.Add(new DataTransfer.InvalidField()
                            {
                                Name = "Email",
                                Code = 4
                            });
                        }
                    }
                }
                else
                {
                    if (Memberinfo.Exists)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Email",
                            Code = 4
                        });
                    }

                    if (string.IsNullOrEmpty(input.Password))
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Password",
                            Code = 1
                        });
                    }
                }

                if (output.IsValid)
                {
                    string passwordHash = "";
                    string passwordSalt = "";

                    if (input.ID.ToInt() == 0)
                    {
                        passwordSalt = Shared.Encryption.GenerateSalt(16);
                        passwordHash = Shared.Encryption.HashPassword(input.Password, passwordSalt);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(input.Password))
                        {
                            passwordHash = Memberinfo.PasswordHash;
                            passwordSalt = Memberinfo.PasswordSalt;
                        }
                        else
                        {
                            passwordHash = Adams.Shared.Encryption.HashPassword(input.Password, Memberinfo.PasswordSalt);
                            passwordSalt = Memberinfo.PasswordSalt;
                        }
                    }

                    var saveinfo = new DataTransfer.Membership.CreateMember.SaveMember()
                    {
                        Member = new Shared.Entities.Membership.Member()
                        {
                            ID = input.ID.ToInt(),
                            FirstName = input.FirstName,
                            LastName = input.LastName,
                            Email = input.Email,
                            IsActive = input.IsActive,
                            IsLocked = input.IsLocked
                        },
                        PasswordHash = passwordHash,
                        PasswordSalt = passwordSalt
                    };

                    DataAccess.Membership.CreateMember.SaveMember(saveinfo);
                    output.IsSaved = true;
                }
            }

            return output;
        }
    }
}
